package cn.agiledata.bank.linkbank.impl.ccb;

import java.math.BigDecimal;

import cn.agiledata.bank.linkbank.impl.AbstractBankNetPayRequest;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;
import cn.agiledata.bank.linkbank.logic.DigitalSignGeneratable;
import cn.agiledata.bank.linkbank.logic.OrderIdTransformer;

/**
 * 中国建设银行外卡网上银行支付请求
 * 
 * @author 马贤波
 * 
 */
public final class CcbIntlNetPayRequest extends AbstractBankNetPayRequest implements
        DigitalSignGeneratable {
	
	private OrderIdTransformer orderIdTransformer;
	
	private OrderIdTransformer getOrderIdTransformer() {
		if (this.orderIdTransformer == null) {
			this.orderIdTransformer = new CcbIntlOrderIdTransformer();
		}
		return this.orderIdTransformer;
	}

    public CcbIntlNetPayRequest() {
        super();
        init();
    }

    private void init() {
    	setParameter(CcbIntlNetConstant.PLAIN_TXT, "1"); // 是否加密：不加密
		setParameter(CcbIntlNetConstant.LANGUAGE, "zh"); // 语言：中文
		setParameter(CcbIntlNetConstant.REGION, "CN"); // 国家：中国
		setParameter(CcbIntlNetConstant.CURRENCY_ID, CcbIntlNetConstant.CURRENCY_RMB); // 币种：人民币
	}

    protected AbstractBankPayRequest getCopiedInstance() {
        return new CcbIntlNetPayRequest();
    }
    
    public void setOrderId(String orderId) {
    	// 建行接口要求订单编号最大6位，进行转换
    	orderId = getOrderIdTransformer().transId2OrderId(orderId);
        setParameter(CcbIntlNetConstant.ORDER_NUMBER, orderId);
    }
    
    public void setAmount(BigDecimal amount) {
        setParameter(CcbIntlNetConstant.PURCHASE_AMOUNT, amount.setScale(2).toString());
    }

    public void setRemark(String remark) {
        setParameter(CcbIntlNetConstant.DESC, "Shanghai Ronsing Network Technology Co.,Ltd.");
    }
    
    public void setMerchantId(String merchantId) {
        setParameter(CcbIntlNetConstant.MERCHANT_ID, merchantId);
    }

    public String getMerchantUrl() {
        return getParameter(CcbIntlNetConstant.MERCHANT_URL);
    }

    public void setMerchantUrl(String merchantUrl) {
    	setParameter(CcbIntlNetConstant.MERCHANT_URL, merchantUrl);
    }
    
    public String getSubmitMethod() {
        return SUBMIT_METHOD_POST;
    }

    public String generateDigitalSign(String cryptoKey) {
        String[] keys = { CcbNetConstant.MERCHANT_ID,
                CcbNetConstant.POS_ID, CcbNetConstant.BRANCH_ID,
                CcbNetConstant.ORDER_ID, CcbNetConstant.AMOUNT,
                CcbNetConstant.CURRENCY_TYPE, CcbNetConstant.TX_CODE,
                CcbNetConstant.REMARK1, CcbNetConstant.REMARK2 };
        String mac = CcbUtils.generateMac(getParameters(), keys);
        return mac;
    }

    public String getCryptoKeyDescription() {
        return null;
    }

    public boolean validateCryptoKey(String cryptoKey) {
        // 不需要密钥，始终返回true
        return true;
    }

}
